﻿//导入表定义
document.write("<script language='javascript' src='/resources/js/views/commonFunction.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineInit.js'></script>");

var page = 1;
var pageSize = 30;
var action = "/report/year/preAlarm.do";
var Province_action="/indicator/type/all.do" //--------------领域

//标题四色块------------------------
var a=
	'&nbsp;&nbsp;&nbsp;&nbsp;<img src="/resources/images/green.png" width="25px;" height="13px"><label>&nbsp;提前达标</label>&nbsp;&nbsp;&nbsp;&nbsp;'+
	'<img src="/resources/images/blue.png" width="25px;" height="13px">&nbsp;<label>达到当年标准&nbsp;&nbsp;</label>&nbsp;&nbsp;&nbsp;&nbsp;'+
	'<img src="/resources/images/yellow.png" width="25px;" height="13px">&nbsp;<label>当年未达标&nbsp;&nbsp;</label>&nbsp;&nbsp;&nbsp;&nbsp;'+
	'<img src="/resources/images/red.png" width="25px;" height="13px">&nbsp;<label>当年未达标且离标准相差大(离当年标准低于50%)终期达标难度大</label>';
//标题四色块end--------------------

function query(curPage, pageSize) {
	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);
	//-----------------------------------------------------------------------------------------------------------------------------------------------------------------参数领域替换省级部门
	// var departid = $('#departid').combobox("getValue");
	var departid=null;
	var areId= $('#Province_action').combobox("getValue");    //领域编号
//	var cityId= $('#city_departmentSelect').combobox("getValue");//城市编号
	//-----------------------------------------------------------------------------------------------------------------------------------------------------------------参数领域替换省级部门

	$('#page').val(curPage);
	$('#pageSize').val(pageSize);
	var forms = $('#forms').val();
	var iname = $('#iname').val();
	var showBackColor = $('#showBackColor').val();

	var yeares=document.getElementsByName("year");
	var year="";
	var yearList= [];
	for (var i=0;i<yeares.length;i++){
		if(yeares[i].checked){
			yearList.push(yeares[i].value);
			year+=yeares[i].value+",";
		}
	}
	if(year!=""){
		year = year.substring(0, year.length-1);
	}

	var reportShowColumns = [
		defineTableReportAllColumns["iname"],
		defineTableReportAllColumns["code"],
		defineTableReportAllColumns["unit"],
		defineTableReportAllColumns["places"],
//		defineTableReportAllColumns["value"],
		defineTableReportAllColumns["sname"],
		defineTableReportAllColumns["aim"],		//目标规划值
		defineTableReportAllColumns["rule"],		//目标规则
		defineTableReportAllColumns["goalDescrip"]	//目标描述
	];


	for(var i=0; i< yearList.length; i++){
		var key = yearList[i]+"showlevel";
		var c = {title:yearList[i], field:yearList[i], width:90,
			formatter: function (value, row, index) {
				return value.substring(0, value.indexOf('_'));
			},
			styler: function (value, row, index) {
//		    		UN_REACH_LOT(1, "完成困难"),
//		    		UN_REACH(2, "未完成"),
//		    		REACH(3, "完成目标"),
//		    		REACH_LOT(4, "超出预期"),
				var showlevel = value.substring(value.indexOf('_')+1, value.length);
				switch(Number(showlevel)) {
					case 1 : return 'background-color:red;';
					case 2 : return 'background-color:#fff66c;';
					case 3 : return 'background-color:#71cbf6;';
					case 4 : return 'background-color:#3ce9b7;';
				}
				return 'background-color:#FFFFFF;';
			}
		};
		reportShowColumns.push(c);
	}

	$.get(action, {"iname":iname, "showBackColor": showBackColor, "departid": departid,"forms": forms,"years": year, "startYear": yearList[0], "status": 1, "pageNow": curPage, "pageSize": pageSize}, function (data) {

		var total = data["size"];
		var totalPage = Math.ceil(total / pageSize);
		var start = (curPage-1) * pageSize;
		var dataLength = 0;

		data = data["results"];
		if(data!=null){
			dataLength = data.length;
		}
		$('#tt').datagrid( {
			title:'报表列表'+a,
			width : '100%',
			height : 'auto',
			fitColumns: true,
			columns: [
				reportShowColumns
			],
			singleSelect:true,// 是否单选
			loadMsg: '数据加载中,请稍后......',
			pageNumber: curPage,//显示数据列用
			pageSize: pageSize,
			pageList: [pageSize, 50, 100],
			rownumbers: true,
			pagination: true,//打开翻页功能
			data: data
		});

		var p = $('#tt').datagrid('getPager');
		$(p).pagination({
			total: total,
			pageList: [pageSize, 50, 100],
			pageSize: pageSize,
			pageNumber: curPage,
			beforePageText: '第',
			afterPageText: '页    共 ' + totalPage + ' 页',
			displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
			onSelectPage: function (curPage, pageSize) {
				query(curPage, pageSize);
			},
			onChangePageSize: function (ps) {
				pageSize = ps;
			}
		});
		try{window.parent.unmask();}catch(err){}
	}, "json");
}

//------------------------------------------------------------------省级领域-------------------------------------------------------------
//获取领域信息  new
function loadDepart(){
// 	ajaxSettings.async = false;
	$.getJSON(Province_action, function (json) {
			// json.data.unshift({
			// 	id : -1 ,
			// 	name : "全部"
			// });
			$('#Province_action').combobox({
				method : 'GET',
				async : false,
				data : json.data,
				editable : false,
				valueField : 'id',
				textField : 'name',
				onLoadSuccess : function() { // 数据加载完毕事件
					var a = $('#Province_action').combobox('getData');
					if (a.length > 0) {
						$("#Province_action").combobox('select', a[0].id);
					}
				},
				onSelect:function () {
					query(page,pageSize);
				}
			})
		},
		'json');
}

$(document).ready(function () {
	CRUD.queryList();
	defineTableInitForm(500,400,"添加上报数据");
});

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------新加统计颜色数量:不要分页，加载所有
function checkChangeImage(){
	var yeares = document.getElementsByName("year");
	var year = "";
	var yearList = [];
	for (var i = 0; i < yeares.length; i++) {
		if (yeares[i].checked) {
			yearList.push(yeares[i].value);
			year += yeares[i].value + ",";
		}
	}
	var showBackColor = $('#showBackColor').val();
	var departid = null;
	//-----------------------------------------------------------------------------------------------------------------------------------------------------------------参数   领域替换省级部门  （放开屏蔽）
	// // var departid = $('#departid').combobox("getValue");
	// var departid=null;
	// var areId= $('#Province_action').combobox("getValue");    //领域编号
	//var cityId= $('#city_departmentSelect').combobox("getValue");//城市编号
	//-----------------------------------------------------------------------------------------------------------------------------------------------------------------参数   领域替换省级部门   （放开屏蔽）
	var forms = $('#forms').val();
	var iname = $('#iname').val();
	$.get(action, {"iname":iname, "showBackColor": showBackColor, "departid": departid,"forms": forms,"years": year, "startYear": yearList[0], "status": 1}, function (data) {

		data = data["results"];
		if (data != null) {
			dataLength = data.length;
		}

		var color_Data = [];   // 颜色分布数组
		for (var j = 0; j < yearList.length; j++) {
			var years_Data = {};
			var red = 0;
			var yellow = 0;
			var blue = 0;
			var green = 0;
			for (var i = 0; i < data.length; i++) {
				var value = data[i][yearList[j]];
				var showlevel = value.substring(value.indexOf('_') + 1, value.length);
				if(Number(showlevel)==1){
					red++;
				}
				else if(Number(showlevel)==2){
					yellow++;
				}
				else if(Number(showlevel)==3){
					blue++;
				}
				else if(Number(showlevel)==4){
					green++;
				}
				else{
					continue;
				}
				years_Data["year"] = yearList[j];
				years_Data["red"] = red;
				years_Data["yellow"] = yellow;
				years_Data["blue"] = blue;
				years_Data["green"] = green;
			}
			color_Data[j] = years_Data;
		}

		var jsonXData=[];    //年份数组 放在图里
		var series=[];
		var data_red=[];
		var data_yellow=[];
		var data_blue=[];
		var data_green=[];
		for(var i=0;i<color_Data.length;i++){
			jsonXData.push(color_Data[i].year);
			data_red.push(color_Data[i].red);
			data_yellow.push(color_Data[i].yellow);
			data_blue.push(color_Data[i].blue);
			data_green.push(color_Data[i].green);
		}
		var series_data_green={"name":"提前达标","type":"bar","data":data_green};
		var series_data_blue={"name":"达到当年标准","type":"bar","data":data_blue};
		var series_data_yellow={"name":"当年未达标","type":"bar","data":data_yellow};
		var series_data_red={"name":"当年未达标且离标准相差大(离当年标准低于50%)终期达标难度大","type":"bar","data":data_red};

		series.push(series_data_green);
		series.push(series_data_blue);
		series.push(series_data_yellow);
		series.push(series_data_red);

		showData(series, jsonXData);

	}, "json");
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------新加统计颜色数量end


//图-----------------------------------------------------------------------------
function showData(series, jsonXData){

	$("#view_containerzhuzi").height("360px");
	$("#view_containerzhuzi").width();
	var myChart = echarts.init(document.getElementById('view_containerzhuzi'));
// 指定图表的配置项和数据
	option = {
		legend: {
			data:['提前达标','达到当年标准','当年未达标','当年未达标且离标准相差大(离当年标准低于50%)终期达标难度大']
		},
		tooltip: {
			trigger: 'axis'
		},
		toolbox: {
			show: true,
			orient:'vertical',
			y:'center',
			itemSize:24,
			feature: {
				mark: {show: true},
				magicType: {show: true, type: ['line', 'bar','stack','tiled','pie','funnel']},
				restore: {show: true},
				saveAsImage : {
					show : true,
					title : '保存为图片',
					type : 'png',
					lang : ['点击保存']
				}
			}
		},
		calculable: true,
		xAxis: [
			{
				type: 'category',
				data: jsonXData
			}
		],
		yAxis: [
			{
				type: 'value'
			}
		],

		series:series,
		color:['#3ce9b7','#71cbf6','#fff66c','red']
	};

	console.info(option);
// 使用刚指定的配置项和数据显示图表。
	myChart.setOption(option);
	$('#view_dark').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'),'dark');
		myChart.setOption(option);
	});
	$('#view_infographic').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'),'infographic');
		myChart.setOption(option);
	});
	$('#view_macarons').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'),'macarons');
		myChart.setOption(option);
	});
	$('#view_vintage').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'),'vintage');
		myChart.setOption(option);
	});
	$('#view_default').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'));
		myChart.setOption(option);
	});
	myChart.setOption(option);
	$('#view_landscape').click(function () {
		myChart.setOption({
			xAxis: [
				{
					type: 'value'
				}
			],
			yAxis: [
				{
					type: 'category',
					data: jsonXData
				}
			]
		})
	});
	myChart.setOption(option);
	$('#view_reset').click(function () {
		myChart.setOption({
			xAxis: [
				{
					type: 'category',
					data: jsonXData
				}
			],
			yAxis: [
				{
					type: 'value'
				}
			]
		})
	});

	$('#view_change').show();
	$('#view_colorSelect').empty();
}

//复选框点击事件
function checkyears(){
	CRUD.queryList();
}

var CRUD = {
	queryList: function () {
		try{window.parent.mask();}catch(err){}
		//不需要二级联动
		loadDepart();
//		loadDepart1();
		checkChangeImage();
	}
};

function Save_Excel() {//导出Excel文件
	//getExcelXML有一个JSON对象的配置，配置项看了下只有title配置，为excel文档的标题
	var data = $('#tt').datagrid('getExcelXml', { title: 'datagrid import to excel' }); //获取datagrid数据对应的excel需要的xml格式的内容
	//用ajax发动到动态页动态写入xls文件中
	var url = '/export/excel.do'; //如果为asp注意修改后缀
	$.ajax({ url: url, data: { data: data }, type: 'POST', dataType: 'text',
		success: function (fn) {
			alert('导出excel成功！');
			window.location = fn; //执行下载操作
		},
		error: function (xhr) {
			alert('动态页有问题\nstatus：' + xhr.status + '\nresponseText：' + xhr.responseText)
		}
	});
	return false;
}
